home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / Tcl 6.2 / tclGet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-29  |  4.9 KB  |  183 lines

  1. #ifdef macintosh
  2. #    pragma segment tclGet
  3. #endif
  4.  
  5. /* 
  6.  * tclGet.c --
  7.  *
  8.  *    This file contains procedures to convert strings into
  9.  *    other forms, like integers or floating-point numbers or
  10.  *    booleans, doing syntax checking along the way.
  11.  *
  12.  * Copyright 1990-1991 Regents of the University of California
  13.  * Permission to use, copy, modify, and distribute this
  14.  * software and its documentation for any purpose and without
  15.  * fee is hereby granted, provided that the above copyright
  16.  * notice appear in all copies.  The University of California
  17.  * makes no representations about the suitability of this
  18.  * software for any purpose.  It is provided "as is" without
  19.  * express or implied warranty.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] = "$Header: /sprite/src/lib/tcl/RCS/tclGet.c,v 1.10 91/09/04 16:53:25 ouster Exp $ SPRITE (Berkeley)";
  24. #endif /* not lint */
  25.  
  26. #include "tclInt.h"
  27.  
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * Tcl_GetInt --
  32.  *
  33.  *    Given a string, produce the corresponding integer value.
  34.  *
  35.  * Results:
  36.  *    The return value is normally TCL_OK;  in this case *intPtr
  37.  *    will be set to the integer value equivalent to string.  If
  38.  *    string is improperly formed then TCL_ERROR is returned and
  39.  *    an error message will be left in interp->result.
  40.  *
  41.  * Side effects:
  42.  *    None.
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46.  
  47. int
  48. Tcl_GetInt(interp, string, intPtr)
  49.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  50.     char *string;        /* String containing a (possibly signed)
  51.                  * integer in a form acceptable to strtol. */
  52.     int *intPtr;        /* Place to store converted result. */
  53. {
  54.     char *end;
  55.     int i;
  56.  
  57.     i = strtol(string, &end, 0);
  58.     while ((*end != '\0') && isspace(*end)) {
  59.     end++;
  60.     }
  61.     if ((end == string) || (*end != 0)) {
  62.     Tcl_AppendResult(interp, "expected integer but got \"", string,
  63.         "\"", (char *) NULL);
  64.     return TCL_ERROR;
  65.     }
  66.     *intPtr = i;
  67.     return TCL_OK;
  68. }
  69.  
  70. /*
  71.  *----------------------------------------------------------------------
  72.  *
  73.  * Tcl_GetDouble --
  74.  *
  75.  *    Given a string, produce the corresponding double-precision
  76.  *    floating-point value.
  77.  *
  78.  * Results:
  79.  *    The return value is normally TCL_OK;  in this case *doublePtr
  80.  *    will be set to the double-precision value equivalent to string.
  81.  *    If string is improperly formed then TCL_ERROR is returned and
  82.  *    an error message will be left in interp->result.
  83.  *
  84.  * Side effects:
  85.  *    None.
  86.  *
  87.  *----------------------------------------------------------------------
  88.  */
  89.  
  90. int
  91. Tcl_GetDouble(interp, string, doublePtr)
  92.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  93.     char *string;        /* String containing a floating-point number
  94.                  * in a form acceptable to strtod. */
  95.     double *doublePtr;        /* Place to store converted result. */
  96. {
  97.     char *end;
  98.     double d;
  99.  
  100.     d = strtod(string, &end);
  101.     while ((*end != '\0') && isspace(*end)) {
  102.     end++;
  103.     }
  104.     if ((end == string) || (*end != 0)) {
  105.     Tcl_AppendResult(interp, "expected floating-point number but got \"",
  106.         string, "\"", (char *) NULL);
  107.     return TCL_ERROR;
  108.     }
  109.     *doublePtr = d;
  110.     return TCL_OK;
  111. }
  112.  
  113. /*
  114.  *----------------------------------------------------------------------
  115.  *
  116.  * Tcl_GetBoolean --
  117.  *
  118.  *    Given a string, return a 0/1 boolean value corresponding
  119.  *    to the string.
  120.  *
  121.  * Results:
  122.  *    The return value is normally TCL_OK;  in this case *boolPtr
  123.  *    will be set to the 0/1 value equivalent to string.  If
  124.  *    string is improperly formed then TCL_ERROR is returned and
  125.  *    an error message will be left in interp->result.
  126.  *
  127.  * Side effects:
  128.  *    None.
  129.  *
  130.  *----------------------------------------------------------------------
  131.  */
  132.  
  133. int
  134. Tcl_GetBoolean(interp, string, boolPtr)
  135.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  136.     char *string;        /* String containing a boolean number
  137.                  * specified either as 1/0 or true/false or
  138.                  * yes/no. */
  139.     int *boolPtr;        /* Place to store converted result, which
  140.                  * will be 0 or 1. */
  141. {
  142.     char c;
  143.     char lowerCase[10];
  144.     int i, length;
  145.  
  146.     /*
  147.      * Convert the input string to all lower-case.
  148.      */
  149.  
  150.     for (i = 0; i < 9; i++) {
  151.     c = string[i];
  152.     if (c == 0) {
  153.         break;
  154.     }
  155.     if ((c >= 'A') && (c <= 'Z')) {
  156.         c += 'a' - 'A';
  157.     }
  158.     lowerCase[i] = c;
  159.     }
  160.     lowerCase[i] = 0;
  161.  
  162.     length = strlen(lowerCase);
  163.     c = lowerCase[0];
  164.     if ((c == '0') && (lowerCase[1] == '\0')) {
  165.     *boolPtr = 0;
  166.     } else if ((c == '1') && (lowerCase[1] == '\0')) {
  167.     *boolPtr = 1;
  168.     } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
  169.     *boolPtr = 1;
  170.     } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
  171.     *boolPtr = 0;
  172.     } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
  173.     *boolPtr = 1;
  174.     } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
  175.     *boolPtr = 0;
  176.     } else {
  177.     Tcl_AppendResult(interp, "expected boolean value but got \"",
  178.         string, "\"", (char *) NULL);
  179.     return TCL_ERROR;
  180.     }
  181.     return TCL_OK;
  182. }
  183.